// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.editors.palette;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.model.DenguesModelManager;
import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DBNote;
import org.dengues.model.database.DBTable;
import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.CreationToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.CreationFactory;

public final class DBEditorPaletteFactory {

    private static PaletteRoot palette;

    public static PaletteRoot createPalette() {
        palette = new PaletteRoot();
        palette.add(createToolsGroup(palette));
        palette.add(createShapesDrawer());
        return palette;
    }

    private static PaletteEntry createShapesDrawer() {
        PaletteDrawer componentsDrawer = new PaletteDrawer(Messages.getString("DBEditorPaletteFactory.Database.Label")); //$NON-NLS-1$
        CombinedTemplateCreationEntry component;
        component = new CombinedTemplateCreationEntry(
                Messages.getString("DBEditorPaletteFactory.Table.Label"), Messages.getString("DBEditorPaletteFactory.CreateTable.Comment"), DBTable.class, //$NON-NLS-1$ //$NON-NLS-2$
                new CreationFactory() {

                    public Object getNewObject() {
                        return DenguesModelManager.getDatabaseFactory().createDBTable();
                    };

                    public Object getObjectType() {
                        return null;
                    };
                }, ImageUtil.getDescriptor(IDenguesSharedImage.VIEW_TABLE), ImageUtil
                        .getDescriptor(IDenguesSharedImage.VIEW_TABLE));
        componentsDrawer.add(component);
        component = new CombinedTemplateCreationEntry(
                Messages.getString("DBEditorPaletteFactory.Column.Label"), Messages.getString("DBEditorPaletteFactory.CreateColumn.Comment"), DBColumn.class, //$NON-NLS-1$ //$NON-NLS-2$
                new CreationFactory() {

                    public Object getNewObject() {
                        DBColumn createDBColumn = DenguesModelManager.getDatabaseFactory().createDBColumn();
                        createDBColumn.setType("INT"); //$NON-NLS-1$
                        return createDBColumn;
                    };

                    public Object getObjectType() {
                        return null;
                    };
                }, ImageUtil.getDescriptor(IDenguesSharedImage.VIEW_COLUMN), ImageUtil
                        .getDescriptor(IDenguesSharedImage.VIEW_COLUMN));
        componentsDrawer.add(component);
        return componentsDrawer;
    }

    private static PaletteContainer createToolsGroup(PaletteRoot palette) {
        PaletteGroup toolGroup = new PaletteGroup(Messages.getString("DBEditorPaletteFactory.Tools.Label")); //$NON-NLS-1$
        // Add a selection tool to the group
        ToolEntry tool = new PanningSelectionToolEntry();
        toolGroup.add(tool);
        palette.setDefaultEntry(tool);

        // Add a marquee tool to the group
        // toolGroup.add(new MarqueeToolEntry());
        CreationToolEntry noteCreationToolEntry = new CreationToolEntry(
                Messages.getString("DBEditorPaletteFactory.Note.Label"), Messages.getString("DBEditorPaletteFactory.CreateNote.Label"), new CreationFactory() { //$NON-NLS-1$ //$NON-NLS-2$

                    public Object getNewObject() {
                        return DenguesModelManager.getDatabaseFactory().createDBNote();
                    }

                    public Object getObjectType() {
                        return DBNote.class;
                    }

                }, ImageUtil.getDescriptor(IDenguesSharedImage.PALETTE_CODE), ImageUtil
                        .getDescriptor(IDenguesSharedImage.PALETTE_CODE));

        ConnectionCreationToolEntry fk = new ConnectionCreationToolEntry(Messages
                .getString("DBEditorPaletteFactory.FK"), Messages.getString("DBEditorPaletteFactory.FK.Comment"), //$NON-NLS-1$ //$NON-NLS-2$
                new CreationFactory() {

                    public Object getNewObject() {
                        return DenguesModelManager.getDatabaseFactory().createForeignKey();
                    }

                    public Object getObjectType() {
                        return null;
                    }
                }, ImageUtil.getDescriptor(IDenguesSharedImage.VIEW_PK), ImageUtil
                        .getDescriptor(IDenguesSharedImage.VIEW_PK));
        fk.setToolClass(CompConnectionCreationTool.class);
        toolGroup.add(noteCreationToolEntry);
        toolGroup.add(fk);
        return toolGroup;
    }
}
